import Data.Char (isAlpha)
import Data.Text (pack, unpack, replace)

parseLine :: String -> Int
parseLine s = 
    10 * (firstDigit s) + (lastDigit s)
    where
        firstDigit = (read :: String -> Int) . take 1 . dropWhile isAlpha
        lastDigit  = firstDigit . reverse

replaceWords :: String -> String
replaceWords = unpack
             . replace "nine"  "nine9nine"
             . replace "eight" "eigeht8eight"
             . replace "seven" "seven7seven"
             . replace "six"   "six6six"
             . replace "five"  "five5five"
             . replace "four"  "four4four"
             . replace "three" "three3three"
             . replace "two"   "two2two"
             . replace "one"   "one1one"
             . pack

main = do
    contents <- readFile "input.txt"
    print $ sum . map parseLine . lines $ contents
    print $ sum . map (parseLine . replaceWords) . lines $ contents
    print $ "Done."